package com.qcby.controller;

import com.qcby.entity.PersonalFavoritesFolder;
import com.qcby.entity.Result;
import com.qcby.entity.Shop;
import com.qcby.service.PersonalCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/personalCollection")
public class PersonalCollectionController {

    @Autowired
    private PersonalCollectionService personalCollectionService;

    @PostMapping("/getUserIdByName")
    @ResponseBody
    public Result<Integer> getUserIdByName(String username){
//        System.out.println("username = " + username);
//        System.out.println(personalCollectionService.getUserIdByName(username));
        return personalCollectionService.getUserIdByName(username);
    }

    @PostMapping("/selectFavoritesList")
    @ResponseBody
    public Result<List<PersonalFavoritesFolder>> selectFavoritesList(Integer u_id,Integer fatherFolderId){
        System.out.println("u_id = " + u_id);
        System.out.println("fatherFolderId = " + fatherFolderId);
        System.out.println(personalCollectionService.selectFavoritesList(u_id, fatherFolderId).toString());

        personalCollectionService.updateShopCountInFolder(u_id, fatherFolderId);
//        System.out.println("更新完店铺数量的文件夹： " + personalCollectionService.updateShopCountInFolder(u_id, fatherFolderId));
        return personalCollectionService.selectFavoritesList(u_id, fatherFolderId);
    }

    @PostMapping("/selectFavoriteShop")
    @ResponseBody
    public Result<List<Shop>> selectFavoriteShop(@RequestParam("u_id") int u_id,@RequestParam("folderId") int fatherFolderId){
        System.out.println("u_id = " + u_id);
        System.out.println("folderId = " + fatherFolderId);
//        System.out.println(personalCollectionService.selectFavoriteShop(u_id,fatherFolderId).toString());
        return personalCollectionService.selectFavoriteShop(u_id,fatherFolderId);
    }

    @PostMapping("/createFolder")
    @ResponseBody
    public Result<PersonalFavoritesFolder> createFolder(PersonalFavoritesFolder folderInfo){
        System.out.println("folderInfo = " + folderInfo);
        folderInfo.setShopCount(0);

        return personalCollectionService.createFolder(folderInfo);
    }
    @PostMapping("/collection")
    @ResponseBody
    public Result<Integer> collection(Integer userId,Integer shopId,String reason,Integer folderId){
        return personalCollectionService.collection(userId,shopId,reason,folderId);
    }

    @PostMapping("/toggleFavoriteShop")
    @ResponseBody
    public Result<Integer> deleteFolder(boolean isCollected, int shopId ,int u_id, int folderId){
        if(isCollected){
            return personalCollectionService.recollecteFolder(shopId, u_id, folderId);
        }else{
            return personalCollectionService.deleteFolder(shopId, u_id, folderId);
        }
    }

    @PostMapping("/updateShopCountInFolder")
    @ResponseBody
    public Result<List<PersonalFavoritesFolder>> updateShopCountInFolder(int userId, int fatherId){
        return personalCollectionService.updateShopCountInFolder(userId, fatherId);
    }

}
